import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

class Student{
    private String name = "zhangsan";
    public int age = 18;
    public Student(){
        System.out.println("无参数构造方法");
    }
    private Student(String name, int age){
        this.name = name;
        this.age = age;
    }

    private void set(String name, int age){
        System.out.println(name + " " + age);
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Demo1 {
    public static void reflectCreateClass(){
        Class<?> a;
        try {
            a = Class.forName("Student");
            Student student = (Student) a.newInstance();

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    public static void createClass(){
        Class<?> a;
        try {
            a = Class.forName("Student");

            //使用反射新建一个对象
            Student student = (Student) a.newInstance();//返回Object
            System.out.println(student);

            //使用反射调用私有构造方法
            Constructor<Student> constructor =
                    (Constructor<Student>) a.getDeclaredConstructor(String.class, int.class);
            constructor.setAccessible(true);
            Student student1 = constructor.newInstance("ll",20);
            System.out.println(student1);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
    //私有属性
    private static void reflectPrivateField(){
        Class<?> a;
        try {
            a = Class.forName("Student");
            Field field = a.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student) a.newInstance();

            field.set(student,"wangwu");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    //私有方法
    private static void reflectPrivateMethod(){
        Class<?> a;
        try {
            a = Class.forName("Student");
            Method method = a.getDeclaredMethod("set", String.class, int.class);
            method.setAccessible(true);
            Student student = (Student) a.newInstance();
            method.invoke(student,"pppp",10);
            //System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args){
        //createClass();
        //reflectPrivateField();
        //reflectPrivateMethod();
    }
}
